﻿using System;
using System.Drawing;
using System.IO;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Script_Text_Editor_WPF_Edition
{
    /// <summary>
    /// WindowSetting.xaml 的交互逻辑
    /// </summary>
    public partial class WindowSetting : Window
    {
        /// <summary>
        /// Sender
        /// </summary>
        /// <param name="sender"></param>
        MainWindow mainWindow;

        public WindowSetting(MainWindow s)
        {
            InitializeComponent();

            mainWindow = s;
        }

        private void buttonClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //Page1
            labelOrgFont.Content = String.Format("{0} {1}", Properties.Settings.Default.textBoxOrgFontName, Properties.Settings.Default.textBoxOrgFontSize);
            labelNewFont.Content = String.Format("{0} {1}", Properties.Settings.Default.textBoxNewFontName, Properties.Settings.Default.textBoxNewFontSize);

            labelOrgFore.Content = Properties.Settings.Default.textBoxOrgFore;
            labelOrgFore.Background = new System.Windows.Media.SolidColorBrush(Properties.Settings.Default.textBoxOrgFore);
            labelOrgBack.Content = Properties.Settings.Default.textBoxOrgBack;
            labelOrgBack.Background = new System.Windows.Media.SolidColorBrush(Properties.Settings.Default.textBoxOrgBack);

            labelNewFore.Content = Properties.Settings.Default.textBoxNewFore;
            labelNewFore.Background = new System.Windows.Media.SolidColorBrush(Properties.Settings.Default.textBoxNewFore);
            labelNewBack.Content = Properties.Settings.Default.textBoxNewBack;
            labelNewBack.Background = new System.Windows.Media.SolidColorBrush(Properties.Settings.Default.textBoxNewBack);

            labelOrgPrev.Foreground = new System.Windows.Media.SolidColorBrush(Properties.Settings.Default.textBoxOrgFore);
            labelOrgPrev.Background = new System.Windows.Media.SolidColorBrush(Properties.Settings.Default.textBoxOrgBack);
            labelNewPrev.Foreground = new System.Windows.Media.SolidColorBrush(Properties.Settings.Default.textBoxNewFore);
            labelNewPrev.Background = new System.Windows.Media.SolidColorBrush(Properties.Settings.Default.textBoxNewBack);

            //Page2
            labellistViewSelection.Content = Properties.Settings.Default.listViewSelection;
            labellistViewSelection.Background = new System.Windows.Media.SolidColorBrush(Properties.Settings.Default.listViewSelection);
            labelMarkedLineBack.Content = Properties.Settings.Default.MarkedLineBack;
            labelMarkedLineBack.Background = new System.Windows.Media.SolidColorBrush(Properties.Settings.Default.MarkedLineBack);

            textBoxBackground.Text = Properties.Settings.Default.BackgroundImage;

            //Page3
            comboBoxAutoSave.Text = Properties.Settings.Default.AutoSaveInterval.ToString();

            //Page4
            int type = Properties.Settings.Default.LibraryType;
            switch (type)
            {
                case 1://DynamicCodeWrapper
                    radioButton1.IsChecked = true;
                    break;
                case 2://NetWrapper
                    radioButton2.IsChecked = true;
                    break;
                //case 3://Win32Wrapper
                //radioButton3.IsChecked = true;
                //    break;
                default:
                    radioButton0.IsChecked = true;
                    break;
            }

            textBoxLibraryFile.Text = Properties.Settings.Default.LibraryPath;
        }

        private void buttonOrgFont_Click(object sender, RoutedEventArgs e)
        {
            FontDialog fd = new FontDialog();
            fd.ShowEffects = false;
            fd.Font = new Font(
                Properties.Settings.Default.textBoxOrgFontName,
                float.Parse(Properties.Settings.Default.textBoxOrgFontSize)
                );

            try
            {
                if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    labelOrgFont.Content = String.Format("{0} {1}", fd.Font.Name, fd.Font.Size);

                    labelOrgPrev.FontFamily = new System.Windows.Media.FontFamily(fd.Font.Name);
                    labelOrgPrev.FontSize = fd.Font.Size;

                    Support.Backend.SettingWriter.Change("textBoxOrgFontName", fd.Font.Name);
                    Support.Backend.SettingWriter.Change("textBoxOrgFontSize", fd.Font.Size.ToString());
                }
            }
            catch (Exception ea)
            {
                System.Windows.MessageBox.Show(ea.Message);
            }
        }

        private void buttonOrgFore_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Media.Color c = Properties.Settings.Default.textBoxOrgFore;

            ColorChooser colorChooser = new ColorChooser { Color = System.Drawing.Color.FromArgb(c.A, c.R, c.G, c.B) };

            if (colorChooser.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                return;
            System.Windows.Media.Color mc = System.Windows.Media.Color.FromArgb(colorChooser.Color.A, colorChooser.Color.R, colorChooser.Color.G, colorChooser.Color.B);

            labelOrgFore.Content = mc;
            labelOrgFore.Background = new System.Windows.Media.SolidColorBrush(mc);

            labelOrgPrev.Foreground = new System.Windows.Media.SolidColorBrush(mc);

            Support.Backend.SettingWriter.Change("textBoxOrgFore", mc.ToString());
        }

        private void buttonOrgBack_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Media.Color c = Properties.Settings.Default.textBoxOrgBack;

            ColorChooser colorChooser = new ColorChooser { Color = System.Drawing.Color.FromArgb(c.A, c.R, c.G, c.B) };

            if (colorChooser.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                return;
            System.Windows.Media.Color mc = System.Windows.Media.Color.FromArgb(colorChooser.Color.A, colorChooser.Color.R, colorChooser.Color.G, colorChooser.Color.B);

            labelOrgBack.Content = mc;
            labelOrgBack.Background = new System.Windows.Media.SolidColorBrush(mc);

            labelOrgPrev.Background = new System.Windows.Media.SolidColorBrush(mc);

            Support.Backend.SettingWriter.Change("textBoxOrgBack", mc.ToString());
        }

        private void buttonNewFont_Click(object sender, RoutedEventArgs e)
        {
            FontDialog fd = new FontDialog();
            fd.ShowEffects = false;
            fd.Font = new Font(
                Properties.Settings.Default.textBoxNewFontName,
                float.Parse(Properties.Settings.Default.textBoxNewFontSize)
                );

            try
            {
                if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    labelNewFont.Content = String.Format("{0} {1}", fd.Font.Name, fd.Font.Size);

                    labelNewPrev.FontFamily = new System.Windows.Media.FontFamily(fd.Font.Name);
                    labelNewPrev.FontSize = fd.Font.Size;

                    Support.Backend.SettingWriter.Change("textBoxNewFontName", fd.Font.Name);
                    Support.Backend.SettingWriter.Change("textBoxNewFontSize", fd.Font.Size.ToString());
                }
            }
            catch (Exception ea)
            {
                System.Windows.MessageBox.Show(ea.Message);
            }
        }

        private void buttonNewFore_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Media.Color c = Properties.Settings.Default.textBoxNewFore;

            ColorChooser colorChooser = new ColorChooser { Color = System.Drawing.Color.FromArgb(c.A, c.R, c.G, c.B) };

            if (colorChooser.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                return;

            System.Windows.Media.Color mc = System.Windows.Media.Color.FromArgb(colorChooser.Color.A, colorChooser.Color.R, colorChooser.Color.G, colorChooser.Color.B);

            labelNewFore.Content = mc;
            labelNewFore.Background = new System.Windows.Media.SolidColorBrush(mc);

            labelNewPrev.Foreground = new System.Windows.Media.SolidColorBrush(mc);

            Support.Backend.SettingWriter.Change("textBoxNewFore", mc.ToString());
        }

        private void buttonNewBack_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Media.Color c = Properties.Settings.Default.textBoxNewBack;

            ColorChooser colorChooser = new ColorChooser { Color = System.Drawing.Color.FromArgb(c.A, c.R, c.G, c.B) };

            if (colorChooser.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                return;

            System.Windows.Media.Color mc = System.Windows.Media.Color.FromArgb(colorChooser.Color.A, colorChooser.Color.R, colorChooser.Color.G, colorChooser.Color.B);

            labelNewBack.Content = mc;
            labelNewBack.Background = new System.Windows.Media.SolidColorBrush(mc);

            labelNewPrev.Background = new System.Windows.Media.SolidColorBrush(mc);

            Support.Backend.SettingWriter.Change("textBoxNewBack", mc.ToString());
        }

        private void buttonlistViewSelection_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Media.Color c = Properties.Settings.Default.listViewSelection;

            ColorChooser colorChooser = new ColorChooser { Color = System.Drawing.Color.FromArgb(c.A, c.R, c.G, c.B) };

            if (colorChooser.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                return;

            System.Windows.Media.Color mc = System.Windows.Media.Color.FromArgb(colorChooser.Color.A, colorChooser.Color.R, colorChooser.Color.G, colorChooser.Color.B);

            labellistViewSelection.Content = mc;
            labellistViewSelection.Background = new System.Windows.Media.SolidColorBrush(mc);

            Support.Backend.SettingWriter.Change("listViewSelection", mc.ToString());
        }

        private void buttonMarkedLineBack_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Media.Color c = Properties.Settings.Default.MarkedLineBack;

            ColorChooser colorChooser = new ColorChooser { Color = System.Drawing.Color.FromArgb(c.A, c.R, c.G, c.B) };

            if (colorChooser.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                return;

            System.Windows.Media.Color mc = System.Windows.Media.Color.FromArgb(colorChooser.Color.A, colorChooser.Color.R, colorChooser.Color.G, colorChooser.Color.B);

            labelMarkedLineBack.Content = mc;
            labelMarkedLineBack.Background = new System.Windows.Media.SolidColorBrush(mc);

            Support.Backend.SettingWriter.Change("MarkedLineBack", mc.ToString());

            //Update listViewMain
            if (mainWindow.dataTable.Rows.Count == 0)
                return;

            for (int i = 0; i < mainWindow.dataTable.Rows.Count; i++)
                if ((bool)mainWindow.dataTable.Rows[i]["Marked"])
                {
                    mainWindow.dataTable.Rows[i]["Marked"] = false;
                    mainWindow.dataTable.Rows[i]["Marked"] = true;
                }
        }

        private void buttonDefault_Click(object sender, RoutedEventArgs e)
        {
            //备份库设置
            int libraryType = Properties.Settings.Default.LibraryType;
            string libraryPath = Properties.Settings.Default.LibraryPath;

            string configFileName = System.Windows.Forms.Application.ExecutablePath + ".config";

            StreamWriter bw = new StreamWriter(new FileStream(configFileName, FileMode.Create), System.Text.Encoding.UTF8);
            bw.Write(Script_Text_Editor_WPF_Edition.Properties.Resources.app_config);
            bw.Close();

            // 刷新应用程序设置
            Properties.Settings.Default.Reload();

            //还原库设置
            Support.Backend.SettingWriter.Change("LibraryType", libraryType.ToString());
            Support.Backend.SettingWriter.Change("LibraryPath", libraryPath);

            this.Close();
        }

        private void buttonBackground_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Support.Backend.SettingWriter.Change("BackgroundImage", ofd.FileName);

                textBoxBackground.Text = ofd.FileName;
            }
        }

        private void buttonLibraryFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                //需要确认
                //Support.Backend.SettingWriter.Change("BackgroundImage", ofd.FileName);

                textBoxLibraryFile.Text = ofd.FileName;
            }
        }

        private void buttonConfirm_Click(object sender, RoutedEventArgs e)
        {
            int type;
            if (radioButton0.IsChecked == true)
                type = 0;
            else if (radioButton1.IsChecked == true)
                type = 1;
            else if (radioButton2.IsChecked == true)
                type = 2;
            else if (radioButton3.IsChecked == true)
                type = 3;
            else
                type = 0;

            Support.Backend.SettingWriter.Change("LibraryType", type.ToString());
            Support.Backend.SettingWriter.Change("LibraryPath", textBoxLibraryFile.Text);
        }

        private void comboBoxAutoSave_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            Support.Backend.SettingWriter.Change("AutoSaveInterval", ((System.Windows.Controls.ComboBoxItem)comboBoxAutoSave.SelectedItem).Content.ToString());

            mainWindow.timer.Interval = new TimeSpan(0, 0, Properties.Settings.Default.AutoSaveInterval);
        }
    }
}